package com.jingdianjichi.auth.domain.service.impl;

import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jingdianjichi.auth.common.connstants.AuthConstant;
import com.jingdianjichi.auth.common.enums.AuthUserStatusEnum;
import com.jingdianjichi.auth.domain.convert.AuthPermissionBOConverter;
import com.jingdianjichi.auth.domain.convert.AuthRoleBOConverter;
import com.jingdianjichi.auth.domain.entity.AuthPermissionBO;
import com.jingdianjichi.auth.domain.entity.AuthRoleBO;
import com.jingdianjichi.auth.domain.entity.AuthUserBO;
import com.jingdianjichi.auth.domain.redis.RedisUtil;
import com.jingdianjichi.auth.domain.service.AuthPermissionDomainService;
import com.jingdianjichi.auth.domain.service.AuthRoleDomainService;
import com.jingdianjichi.auth.infra.basic.entity.AuthPermission;
import com.jingdianjichi.auth.infra.basic.entity.AuthRole;
import com.jingdianjichi.auth.infra.basic.entity.AuthUser;
import com.jingdianjichi.auth.infra.basic.service.AuthRoleService;
import com.jingdianjichi.auth.infra.basic.service.impl.AuthPermissionServiceImpl;
import com.jingdianjichi.subject.common.enums.IsDeletedFlagEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 86156
 * @version 1.0
 * @date 2024-10-31 9:55
 */
@Service
@Slf4j
public class AuthPermissionDomainServiceImpl implements AuthPermissionDomainService {

    @Autowired
    private AuthPermissionServiceImpl authPermissionService;
    @Resource
    private RedisUtil redisUtil;

    private String authPermissionPrefix = "auth.permission";

    /**
     * 新增权限
     */
    @Override
    public Boolean add(AuthPermissionBO authPermissionBO) {
        if(log.isInfoEnabled()){
            log.info("AuthPermissionDomainServiceImpl.add.bo: {}", JSON.toJSONString(authPermissionBO));
        }
        AuthPermission authPermission = AuthPermissionBOConverter.instance.authPermissionBoToEntity(authPermissionBO);
        authPermission.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        authPermission.setStatus(AuthUserStatusEnum.OPEN.getCode());
        int count = authPermissionService.insert(authPermission);
        return count>0;
    }

    /**
     * 更新权限信息
     */
    @Override
    public Boolean update(AuthPermissionBO authPermissionBO) {
        if(log.isInfoEnabled()){
            log.info("AuthPermissionDomainServiceImpl.update.bo: {}", JSON.toJSONString(authPermissionBO));
        }
        AuthPermission authPermission = AuthPermissionBOConverter.instance.authPermissionBoToEntity(authPermissionBO);
        Integer count = authPermissionService.update(authPermission);
        return count>0;
    }

    /**
     * 删除权限信息
     */
    @Override
    public Boolean delete(AuthPermissionBO authPermissionBO) {
        if(log.isInfoEnabled()){
            log.info("AuthPermissionDomainServiceImpl.delete.bo: {}", JSON.toJSONString(authPermissionBO));
        }
        AuthPermission authPermission = AuthPermissionBOConverter.instance.authPermissionBoToEntity(authPermissionBO);
        authPermission.setIsDeleted(IsDeletedFlagEnum.DELETED.getCode());
        Integer count = authPermissionService.update(authPermission);
        return count>0;
    }

    /**
     * 启用/禁用
     */
    @Override
    public Boolean changeStatus(AuthPermissionBO authPermissionBO) {
        if(log.isInfoEnabled()){
            log.info("AuthPermissionDomainServiceImpl.delete.bo: {}", JSON.toJSONString(authPermissionBO));
        }
        AuthPermission authPermission = AuthPermissionBOConverter.instance.authPermissionBoToEntity(authPermissionBO);
        Integer count = authPermissionService.update(authPermission);
        return count>0;
    }

    /**
     * 获取用户权限
     */
    @Override
    public List<String> getPermission(String userName) {
        String permissionKey = redisUtil.buildKey(authPermissionPrefix, userName);
        String permissionValue = redisUtil.get(permissionKey);
        if(StringUtils.isBlank(permissionValue)){
            return new ArrayList<>();
        }
        List<AuthPermission> roleList = new Gson().fromJson(permissionValue, new TypeToken<List<AuthPermission>>() {
        }.getType());
        List<String> authList = roleList.stream().map(AuthPermission::getPermissionKey).collect(Collectors.toList());
        return authList;
    }


}
